package com.ruoyi.project.loan.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.loan.domain.LoanApplication;
import com.ruoyi.project.loan.service.ILoanApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 贷款申请Controller
 *
 * @author ruoyi
 * @date 2025-11-11
 */
@RestController
@RequestMapping("/loan/application" )
public class LoanApplicationController extends BaseController {
    @Autowired
    private ILoanApplicationService loanApplicationService;

    /**
     * 查询贷款申请列表(不分页)
     */
    @PreAuthorize("@ss.hasPermi('loan:application:list')" )
    @GetMapping("/all/list" )
    public AjaxResult allList(LoanApplication loanApplication) {
        List<LoanApplication> list = loanApplicationService.selectLoanApplicationList(loanApplication);
        return success(list);
    }

    /**
     * 查询贷款申请列表
     */
    @PreAuthorize("@ss.hasPermi('loan:application:list')" )
    @GetMapping("/list" )
    public TableDataInfo list(LoanApplication loanApplication) {
        startPage();
        List<LoanApplication> list = loanApplicationService.selectLoanApplicationList(loanApplication);
        return getDataTable(list);
    }

    /**
     * 导出贷款申请列表
     */
    @PreAuthorize("@ss.hasPermi('loan:application:export')" )
    @Log(title = "贷款申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export" )
    public void export(HttpServletResponse response, LoanApplication loanApplication) {
        List<LoanApplication> list = loanApplicationService.selectLoanApplicationList(loanApplication);
        ExcelUtil<LoanApplication> util = new ExcelUtil<LoanApplication>(LoanApplication.class);
        util.exportExcel(response, list, "贷款申请数据" );
    }

    /**
     * 获取贷款申请详细信息
     */
    @PreAuthorize("@ss.hasPermi('loan:application:query')" )
    @GetMapping(value = "/{applicationId}" )
    public AjaxResult getInfo(@PathVariable("applicationId" ) Long applicationId) {
        return success(loanApplicationService.selectLoanApplicationByApplicationId(applicationId));
    }

    /**
     * 新增贷款申请
     */
    @PreAuthorize("@ss.hasPermi('loan:application:add')" )
    @Log(title = "贷款申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LoanApplication loanApplication) {
        return toAjax(loanApplicationService.insertLoanApplication(loanApplication));
    }

    /**
     * 修改贷款申请
     */
    @PreAuthorize("@ss.hasPermi('loan:application:edit')" )
    @Log(title = "贷款申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LoanApplication loanApplication) {
        return toAjax(loanApplicationService.updateLoanApplication(loanApplication));
    }

    /**
     * 删除贷款申请
     */
    @PreAuthorize("@ss.hasPermi('loan:application:remove')" )
    @Log(title = "贷款申请", businessType = BusinessType.DELETE)
    @DeleteMapping("/{applicationIds}" )
    public AjaxResult remove(@PathVariable Long[] applicationIds) {
        return toAjax(loanApplicationService.deleteLoanApplicationByApplicationIds(applicationIds));
    }
}
